<?php
class Wap extends IController
{
	//启动页面
	public function index()
	{
		$this->redirect('/site/index');
		//die('close');
		//环境判断
		$env = IClient::getDevice();//默认pc
		if ( $env == 'mobile' ) {
			if ( IReq::get('client') == 'ios' || IReq::get('client') == 'android' ) {
				$env = 'app';
			} else {
				$env = IClient::isWechat() ? 'we' : 'wap';
			}
		}
		ICookie::set('env',$env);

		switch ( $env ) {
			case 'we':
				// 微信端模拟登陆 通过unionid
				$oauthId = ISession::get('oauth_id');
				$userInfo = ISession::get('oauth_userInfo');
				$userObj      = new IModel('user');
				$oauthUserObj = new IModel('oauth_user');
				$oauthUserRow = $oauthUserObj->getObj("oauth_user_id = '{$userInfo['id']}' and oauth_id = '{$oauthId}' ",'user_id');
				if($oauthUserRow)
				{
					//清理oauth_user和user表不同步匹配的问题
					$tempRow = $userObj->getObj("id = '{$oauthUserRow['user_id']}'");
					if(!$tempRow)
					{
						$oauthUserObj->del("oauth_user_id = '{$userInfo['id']}' and oauth_id = '{$oauthId}' ");
					}
				}

				//存在绑定账号oauth_user与user表同步正常！
				if(isset($tempRow) && $tempRow)
				{
					$userRow = plugin::trigger("isValidUser",array($tempRow['username'],$tempRow['password']));
					plugin::trigger("userLoginCallback",$userRow);
				}
				break;
			case 'app':
				// APP端模拟登陆 通过token
				$token = IReq::get('token');
				if ($token) {
					$user_id = Common::think_decrypt($token);
					//调用_userInfo模拟登录插件
					$result = plugin::trigger('userMoniLoginAct',$user_id);
				}

				break;
			default:

				break;
		}

		//初始化
		$init_view = IReq::get('view');
		$init_page = IReq::get('page');
		$init_view = $init_view ? $init_view : 'mallView';
		$init_page = $init_page ? urldecode($init_page) : '/yunsite/index';
		$init_page = IUrl::creatUrl($init_page);
		$this->init_page = $init_view.'.router.loadPage(\''.$init_page.'\')';

		$this->redirect('index');
	}
}